home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 231 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: qsort help
  5. Date: 3 Jan 1996 14:44:34 GMT
  6. Organization: The Internet Access Group, Inc.
  7. Message-ID: <4ce4oi$hsd@news.iag.net>
  8. References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu> <4cdomq$jgt@dub-news-svc-5.compuserve.com>
  9. NNTP-Posting-Host: pm2-orl9.iag.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.6
  13.  
  14. In article <4cdomq$jgt@dub-news-svc-5.compuserve.com>, 
  15. stuart.hayes@baesema.co.uk says...
  16. >
  17. >xiaoyi@bmecg.bme.ohio-state.edu (Xiaoyi Wu) wrote:
  18. >
  19. >>Hi, I am having some problems with the qsort routine.
  20. >
  21. >
  22. >>int (*compar)(int *a, int *b)
  23. >Function declaration should be:
  24. >int compar(int *a, int *b)
  25.  
  26. If this function is to be passed to qsort, it needs to be something like:
  27.  
  28. int compar( const void *a, const void *b)
  29. {
  30.   const int *c = a, *d = b;
  31.   return *c - *d;
  32. }
  33.  
  34.  
  35. <snip>
  36. >
  37. >Alternatively to declaring function in header file could just declare
  38. >in the function below:
  39. >>int return_moved_contour(int ix, int iy)
  40. >>{
  41. >int (*compar)(int *a, int *b);
  42.  
  43. This defines compar as a pointer to a function that accepts two int pointers
  44. and returns an int.  It is uninitialized.  So, it does not actually point to
  45. any predictable memory location (like a valid function).  If you pass this to 
  46. qsort, ... Well, lets just say that the results might not be what you want
  47. (Assuming that your compiler let you pass it, since it is not what qsort is 
  48. defined to accept)
  49.  
  50. >>  int i, ac, count=0;
  51. >>  int y_coord[10]; /* stores y coordinates whose x == ix */
  52. >
  53. >>  for (ac=0; ac<areaindex; ac++)
  54. >>    {
  55. >>      for (i=0; i<index[ac]; i++)
  56. >>       {
  57. >>         if (xpos[ac][i] == ix) y_coord[count++] = ypos[ac][i];
  58. >>       }
  59. >>      if (count == 0) continue; /* not in this contour */
  60. >>      else {
  61. >>       qsort(y_coord, count, sizeof(int), compar);
  62. <snip>
  63.  
  64. -- 
  65. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  66. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  67.  
  68.